﻿package com.teaphy.coroutinecrawler.build_coroutine

import kotlinx.coroutines.*

fun main() = runBlocking {

    println("coroutineScope -- runBlocking: $this")

    launch(Dispatchers.IO) {

        println("coroutineScope -- launch: $this")

        launch {
            val data = getData()

            println("data : $data")
        }
    }
    launch {
        supervisorScope {
            println("coroutineScope -- supervisorScope2: $this")

            // 子协程的失败不会导致此CoroutineScope失败，也不会影响其他子协程
            launch {
                delay(1000)
                throw Exception("1111")

            }

            launch {
                delay(2000)
                println("coroutineScope -- supervisorScope2 - launch2")
            }
        }

    }


    println("end")
}

suspend fun getData(): Int {
    return supervisorScope {
        println("coroutineScope -- supervisorScope: $this")
        delay(2000)
        1
    }
}